/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.maternity.componentes.grid;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Aluno
 */
public class PersonalGrid {

    private String id;
    private String name;
    private String title;
    private String subTitle;
    private List<Object> listSource;
    private List<PersonalGridColumn> columns;
    private PersonalGridStyle rowStyle;
    private int borderLength;
    private String emptyDataMessage;

    public PersonalGrid(String _id, String _name) {
        id = _id;
        name = _name;
        title = "";
        subTitle = "";
        emptyDataMessage = "Não foram encontrados registros.";
        rowStyle = new PersonalGridStyle();
    }

    public PersonalGrid(String _id, String _name, List<Object> _listSource, List<PersonalGridColumn> _columns, PersonalGridStyle _rowStyle) {
        id = _id;
        name = _name;
        title = "";
        subTitle = "";
        emptyDataMessage = "Não foram encontrados registros.";
        listSource = _listSource;
        columns = _columns;
        rowStyle = _rowStyle;
    }

    private String parseAndRender(Object o, String formatStyle) {
        //checking object type to get a formatted value;
        String renderValue;
        if (o instanceof Date) {
            SimpleDateFormat formatter = new SimpleDateFormat(formatStyle);
            renderValue = formatter.format(o);
        } else if (o instanceof Float) {
            if (formatStyle.equals("m")) {
                renderValue = java.text.NumberFormat.getCurrencyInstance().format((Float) o);
            } else {
                renderValue = String.valueOf((Float) o);
            }
        } else {
            renderValue = String.valueOf(o);
        }

        return renderValue;
    }

    public String render(HttpServletRequest request) throws Exception {
        // checking object pendencies
        if (listSource == null) {
            return "";
        }
        if (columns == null) {
            return "";
        }
        if (columns.isEmpty()) {
            return "";
        }
        if (rowStyle == null) {
            return "";
        }

        // setting table name
        String s = "<h2>" + title + "</h2>";
        s += "<h4>" + subTitle + "</h4>";

        s += "<table id='" + id + "' name = '" + name + "' style='border: " + rowStyle.getBorderLength() + "; width: " + rowStyle.getWidth() + "%;'>";

        // setting collumns title
        s += "<tr style='background-color: " + rowStyle.getColumnsRowStyle() + "; color: " + rowStyle.getColumnsFontStyle() + ";'>";
        for (PersonalGridColumn c : columns) {
            if (c.getWidth() > 0) {
                s += "<td style='width: " + c.getWidth() + "%;'>" + c.getTitle() + "</td>";
            } else {
                s += "<td>" + c.getTitle() + "</td>";

            }
        }
        s += "</tr>";
        // rendering all grid lines
        boolean alternateRow = false;
        for (Object o : listSource) {
            if (!alternateRow) {
                s += "<tr style='background-color: " + rowStyle.getNormalRowStyle() + "; color: " + rowStyle.getNormalFontStyle() + ";'>";
            } else {
                s += "<tr style='background-color: " + rowStyle.getAlternateRowStyle() + "; color: " + rowStyle.getNormalFontStyle() + ";'>";
            }

            alternateRow = !alternateRow;

            Class c = o.getClass();

            for (PersonalGridColumn gc : columns) {
                Method m = c.getMethod(gc.getProperySourceName(), new Class[]{});
                Object propertyValue = m.invoke(o, new Object[]{});

                String renderValue = "";
                if (gc.isLink()) {
                    String link = gc.getRedirectLink();
                    for (String qs : gc.getQueryStringFields()) {
                        Object qsPropertyValue = c.getMethod(qs, new Class[]{}).invoke(o, new Object[]{});
                        String qsValue = parseAndRender(qsPropertyValue, "");
                        for (int i = 0; i < link.length(); i++) {
                            if (link.charAt(i) == '@') {
                                link = link.substring(0, i) + qsValue + link.substring(i + 1, link.length());
                                break;
                            }
                        }
                    }
                    renderValue = "<a href='" + link + "'>";
                }

                renderValue += parseAndRender(propertyValue, gc.getFormatStyle());

                if (gc.isLink()) {
                    renderValue += "</a>";
                }

                s += "<td>" + renderValue + "</td>";
            }

            s += "</tr>";
        }

        if (listSource.isEmpty()) {
            s += "<tr><td colspan='" + columns.size() + "'>" + emptyDataMessage + "</td></tr>";
        }
        s += "</table>";

        if (request != null) {
            s += "<br />";
            s += "<a class='botao' href='" + request.getContextPath() + "/exportPDF'>Exportar PDF</a>";

            request.getSession(true).setAttribute("PDF", this);
        }
        return s;
    }
}
